package com.liancheng.smarthome;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.httpapi.HttpHelper;
import com.liancheng.smarthome.manager.HttpManager;
import com.liancheng.smarthome.utils.logs.LogTag;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 全局application处理器
 */
public class IApplication extends Application {

    private static Application app;

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.color_white, R.color.color_black1);//全局设置主题颜色
                ClassicsHeader header = new ClassicsHeader(context);
                header.setEnableLastTime(false);
                return header;//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        // 初始化 QHttpApi 使用的 HttpHelper，包括地址，拦截器等
        new HttpHelper.Builder(this)
                .initOkHttp()
                .createRetrofit(HttpManager.getIpPort())
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        //请求之前拦截
                        Request request = chain.request();
                        Request.Builder header = request.newBuilder();
//                                    .header("X-Time", String.valueOf(System.currentTimeMillis()))
//                      header.addHeader(Constant.HeaderContent.Language,headerLanguage);
                        Request build = header.build();
                        LogTag.d(build.url().toString());
//                                        LogTag.d("url:" + build.url().url());
//                                        LogTag.d("headers:"+build.headers());
//                                        if(build.body() != null){
//                                            LogTag.d("body:"+build.body().contentType());
//                                        }
                        return chain.proceed(build);
                    }
                })
                .build();
    }

    public static void init(Application application){
        app = application;
    }

    public static Application getInstance(){
        return app;
    }
}